package com.select.course.controller;


import com.select.course.entity.Admin;
import com.select.course.entity.LoginUser;
import com.select.course.service.AdminService;
import com.select.course.service.UserStudentService;
import com.select.course.service.UserTeacherService;
import com.select.course.utils.R;
import com.select.course.vo.AdminVo;
import com.select.course.vo.SumarizeVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


@RequestMapping("/admin")
@CrossOrigin
@RestController
@Slf4j
//@Log4j2
public class AdminController {
//    private static Logger log= Logger.getLogger(类名称.class);

    @Resource
    private AdminService adminService;
    @Resource
    private UserTeacherService userTeacherService;
    @Resource
    private UserStudentService userStudentService;


    @GetMapping("/test")
    @PreAuthorize("hasRole('fasfsa')")
    public String myTest() {

        return "aaaaa";
    }

    @PreAuthorize("hasRole('root')")
    @PutMapping("/updateself")
    public R updateSelf(@RequestBody AdminVo adminVo) {
        R r = adminService.updateSelf(adminVo);
        return r;
    }

    @PreAuthorize("hasRole('root')")
    @PutMapping("/updateselfpwd")
    public R updatePwd(@RequestBody AdminVo adminVo) {
        R r = adminService.updatePwd(adminVo);
        return r;
    }

    @PostMapping("/login_admin")
    public R loginAdmin(@RequestBody AdminVo adminVo) {
        try {
            R r = adminService.loginAdmin(adminVo);
            log.info("用户{}登录成功！", adminVo.getUserId());
            return r;
        } catch (RuntimeException e) {
            log.error("用户{}登录失败:", adminVo.getUserId(), e.getMessage());
        }
        return R.error();
    }

    @GetMapping("/loginout_admin")
    public R loginOutAdmin() {
        LoginUser instance = LoginUser.getInstance();
        try {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
            LoginUser principal = (LoginUser) authentication.getPrincipal();
            principal.getUser().getIdNumber();
            // 用此Id去查找相应的token，然受删除，此处我直接删除ThreadLocal中的数据
//        AdminServiceImpl.toThreadLocal.remove();
            log.info("用户{}退出登录成功", instance.getUser().getIdNumber());
            instance.setAuthorities(null);
            return R.ok("退出登录成功！");
        } catch (RuntimeException e) {
            log.error("用户{}退出登录失败", instance.getUser().getIdNumber());
        }
        return R.error();
    }

    @PreAuthorize("hasRole('root')")
    @GetMapping("getnums")
    public R adminGetNums() {
        SumarizeVo sumarizeVo = adminService.getAdminGetNums();
        return R.ok().put("data", sumarizeVo);
    }

    @PreAuthorize("hasRole('root')")
    @GetMapping("/getself/{userId}")
    public R getSelf(@PathVariable String userId) {
        AdminVo adminVo = adminService.getSelf(userId);
        return R.ok().put("data", adminVo);
    }

    public List<Admin> list() {
        List<Admin> list = adminService.list();
        return list;
    }


}
